package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.BrandMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Brand;
import com.qingcheng.service.goods.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> findAll() {
        //查询所有记录
        return brandMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<Brand> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Brand> brands = (Page<Brand>)brandMapper.selectAll();
        return new PageResult<Brand>(brands.getTotal(),brands.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        //selectByExample : 根据条件查询
        return brandMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        Example example = createExample(searchMap);
        PageHelper.startPage(page,size);
        Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);
        return new PageResult<Brand>(brands.getTotal(),brands.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    /**
     * 修改
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        //Selective --- 忽略属性值为null的数据，即参数为空时，不改变数据表中对应的字段值
        //不加Selective,会直接插入数据，不会对null进行过滤
        brandMapper.updateByPrimaryKeySelective(brand);
    }


    @Override
    public void deleteById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByPrimaryKey(Brand brand) {
        brandMapper.deleteByPrimaryKey(brand);
    }


    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String,Object> searchMap){
        //Brand.class ---> 根据table 注解，定位查找到的表
        //public Example(Class<?> entityClass)
        Example example = new Example(Brand.class);
        //criteria 相当于where，拼接查询条件
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            //名称条件
            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                //品牌名模糊查询
                criteria.andLike("name","%"+(String)searchMap.get("name")+"%");
            }
            //首字母
            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
                //Criteria andEqualTo(Object param): 将此对象的不为空的字段参数作为查询条件
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        return example;
    }
}
